package com.binguo.service;

import com.binguo.common.exception.ServiceException;

import com.binguo.model.mapper.*;
import com.binguo.model.model.BinguoOrder;
import com.binguo.model.model.Shop;
import com.binguo.model.model.ShopQucanguiSmsConfig;
import com.binguo.model.model.SideBoardCellDetail;
import com.binguo.model.wxapi.vo.SMSOrderVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopQucanguiSmsConfigService {

    private static final Logger logger = LoggerFactory.getLogger(ShopQucanguiSmsConfigService.class);
    @Resource
    private ShopQucanguiSmsConfigMapper shopQucanguiSmsConfigMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private SideBoardCellDetailMapper sideBoardCellDetailMapper;
    @Resource
    private BinguoOrderMapper binguoOrderMapper;




    /**
     * 查找出超时的订单
     * @return
     */
    public List<SMSOrderVO>  getAllCustomerInfo(){
        //shop 表:手机号
        List<Shop> shopList = shopMapper.getAllMealCellShop();
        if(null == shopList || shopList.size() <= 0){
            throw new ServiceException("无拥有取餐柜的商户");
        }
        List<ShopQucanguiSmsConfig> shopQucanguiSmsConfigList = shopQucanguiSmsConfigMapper.getAllShopSetting();
        if(null == shopQucanguiSmsConfigList || shopQucanguiSmsConfigList.size() <= 0){
            throw new ServiceException("商户未对短信发送做出设置");
        }
        List<SMSOrderVO> smsOrderVOs = new ArrayList<>();
        for(ShopQucanguiSmsConfig shopQucanguiSmsConfig : shopQucanguiSmsConfigList){
            List<SMSOrderVO> smsOrderVOList = sideBoardCellDetailMapper.selectSMSOrder(shopQucanguiSmsConfig);
            smsOrderVOs.addAll(smsOrderVOList);
        }
        if(null == smsOrderVOs || smsOrderVOs.size() <= 0){
            throw new ServiceException("无未取出的订单");
        }
        List<String> orderNoList = new ArrayList<>();
        for(SMSOrderVO smsOrderVO:smsOrderVOs ){
            orderNoList.add(smsOrderVO.getOrderId());
        }
        List<BinguoOrder> binguoOrders = binguoOrderMapper.selectByOrderNoList(orderNoList) ;
        if(null == binguoOrders || binguoOrders.size() <= 0 ){
            throw new ServiceException("订单异常无手机号");
        }
        for(SMSOrderVO smsOrderVO:smsOrderVOs ){
            for(BinguoOrder binguoOrder : binguoOrders){
                if(binguoOrder.getOrderno().equals(smsOrderVO.getOrderId())){
                    smsOrderVO.setCustomerPhone(binguoOrder.getCustomerphone());
                    smsOrderVO.setOpenId(binguoOrder.getOpenid());
                    smsOrderVO.setShopId(binguoOrder.getShopid());
                    smsOrderVO.setBranchId(binguoOrder.getBranchid());
                    smsOrderVO.setOrderDetailUtil(binguoOrder.getNote()== null ? "":binguoOrder.getNote());
                }
            }
            for(Shop shop : shopList){
                if(shop.getId()==smsOrderVO.getShopId()){
                    smsOrderVO.setShopPhone(shop.getPhone());
                }
            }
        }
        return smsOrderVOs;
    }


    /**
     * 查找出符合条件的订单
     * @return
     */
    public List<SMSOrderVO> getCustomerInfoBySetting(){
        //shop 表:手机号
        List<Shop> shopList = shopMapper.getAllMealCellShop();
        if(null == shopList || shopList.size() <= 0){
            throw new ServiceException("无拥有取餐柜的商户");
        }
        List<ShopQucanguiSmsConfig> shopQucanguiSmsConfigList = shopQucanguiSmsConfigMapper.getAllShopSetting();
        if(null == shopQucanguiSmsConfigList || shopQucanguiSmsConfigList.size() <= 0){
            throw new ServiceException("商户未对短信发送做出设置");
        }
        List<SMSOrderVO> smsOrderVOs = new ArrayList<>();
        for(ShopQucanguiSmsConfig shopQucanguiSmsConfig : shopQucanguiSmsConfigList){
        List<SMSOrderVO> smsOrderVOList = sideBoardCellDetailMapper.selectSMSOrderBySetting(shopQucanguiSmsConfig);
            smsOrderVOs.addAll(smsOrderVOList);
        }
        if(null == smsOrderVOs || smsOrderVOs.size() <= 0){
            throw new ServiceException("无未取出的订单");
        }
        List<String> orderNoList = new ArrayList<>();
        for(SMSOrderVO smsOrderVO:smsOrderVOs ){
            orderNoList.add(smsOrderVO.getOrderId());
        }
        List<BinguoOrder> binguoOrders = binguoOrderMapper.selectByOrderNoList(orderNoList);
        if(null == binguoOrders || binguoOrders.size() <= 0 ){
            throw new ServiceException("订单异常无手机号");
        }
        for(SMSOrderVO smsOrderVO:smsOrderVOs ){
            for(BinguoOrder binguoOrder : binguoOrders){
                if(binguoOrder.getOrderno().equals(smsOrderVO.getOrderId()) && binguoOrder.getOrdertype() == 10){
                    smsOrderVO.setCustomerPhone(binguoOrder.getCustomerphone());
                    smsOrderVO.setOpenId(binguoOrder.getOpenid());
                    smsOrderVO.setOrderDetailUtil(binguoOrder.getNote()== null? "":binguoOrder.getNote());
                }
            }
            for(Shop shop : shopList){
                if(shop.getId()==smsOrderVO.getShopId()){
                    smsOrderVO.setShopPhone(shop.getPhone());
                }
            }
        }
        return smsOrderVOs;
    }


}
